package com.examribbon.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author :admin
 * @description: TODO edit class Description
 * @create date: 2019/4/6 17:34
 */
@RestController
public class ExamRibbonController {

    @Autowired
    RestTemplate restTemplate;

//    测试rebbon使用
    @RequestMapping("/sayhello")
    public String sayhello() {
        String url="http://exam/hello";
        String result = restTemplate.getForEntity(url, String.class).getBody();
        System.out.println("return info : "+result);
        return result;
    }

//    测试熔断器调用服务失败
    @HystrixCommand(fallbackMethod = "showErrorMsg")
    @RequestMapping("/sayNo/{mname}")
    public String sayHelloFailTest(@PathVariable String mname) {
        String url="http://exam/"+mname;
        String result = restTemplate.getForEntity(url, String.class).getBody();
        System.out.println("return info : "+result);
        return result;
    }

    public String showErrorMsg(String methodName){
        return methodName+" 调用失败了";
    }
}
